/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkl53.view;

import java.awt.CardLayout;
import java.awt.Component;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import pkl53.controller.B1cobaJpaController;
import pkl53.controller.B5cobaJpaController;
import pkl53.controller.BackupKuesioner;
import pkl53.controller.Blok5678cobaJpaController;
import pkl53.controller.KuesionerController;

/**
 *
 * @author User
 */
public class DataEntriPekerjaanPanel extends javax.swing.JPanel {

    private static int jumlahPekerja;
    private int loop = 1;
    
    static void setJumlahPekerja(int parseInt) {
        jumlahPekerja = parseInt;
    }

    CardLayout card;
    private JPanel panel;
    private KuesionerController kuesionerController;

    /**
     * Creates new form dataEntriPanel
     */
    public DataEntriPekerjaanPanel() {
        initComponents();
        kuesionerController = new KuesionerController();
        kuesionerController.setBlok123(blok1231);
        kuesionerController.setBlok4(blok41);
        kuesionerController.setBlok56(blok561);
//kuesionerController.setBlok5678(blok56781);

        blok561.setName("blok56");
    }

    public void setmainFrame(JPanel panel) {
        this.panel = panel;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        b1coba1 = new pkl53.model.B1coba();
        blok5678coba1 = new pkl53.model.Blok5678coba();
        b5coba1 = new pkl53.model.B5coba();
        backgroundLain1 = new pkl53.imageClass.BackgroundLain();
        jScrollPane1 = new javax.swing.JScrollPane();
        containerPanel = new javax.swing.JPanel();
        blok1231 = new pkl53.view.Blok123();
        blok41 = new pkl53.view.Blok4();
        blok561 = new pkl53.view.Blok56();
        blok56781 = new pkl53.view.Blok5678();
        nextButton = new javax.swing.JButton();
        previousButton = new javax.swing.JButton();
        homeButton = new javax.swing.JButton();
        Save = new javax.swing.JButton();
        Submit = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(1368, 768));

        jScrollPane1.setOpaque(false);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(1070, 1070));

        containerPanel.setBackground(new java.awt.Color(204, 255, 204));
        containerPanel.setOpaque(false);
        containerPanel.setLayout(new java.awt.CardLayout());
        containerPanel.add(blok1231, "card2");
        containerPanel.add(blok41, "card3");
        containerPanel.add(blok561, "card5");
        containerPanel.add(blok56781, "blok5678");

        jScrollPane1.setViewportView(containerPanel);

        nextButton.setText("Next");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        previousButton.setText("Previous");
        previousButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousButtonActionPerformed(evt);
            }
        });

        homeButton.setText("Home");
        homeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeButtonActionPerformed(evt);
            }
        });

        Save.setText("Save");
        Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveActionPerformed(evt);
            }
        });

        Submit.setText("Submit");
        Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Data Entri");

        javax.swing.GroupLayout backgroundLain1Layout = new javax.swing.GroupLayout(backgroundLain1);
        backgroundLain1.setLayout(backgroundLain1Layout);
        backgroundLain1Layout.setHorizontalGroup(
            backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(backgroundLain1Layout.createSequentialGroup()
                .addGroup(backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(backgroundLain1Layout.createSequentialGroup()
                        .addGap(128, 128, 128)
                        .addGroup(backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(backgroundLain1Layout.createSequentialGroup()
                                .addComponent(Save, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Submit, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(homeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1085, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(backgroundLain1Layout.createSequentialGroup()
                        .addGap(119, 119, 119)
                        .addComponent(jLabel1)))
                .addContainerGap(155, Short.MAX_VALUE))
        );
        backgroundLain1Layout.setVerticalGroup(
            backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, backgroundLain1Layout.createSequentialGroup()
                .addGap(0, 56, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(backgroundLain1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nextButton)
                    .addComponent(previousButton)
                    .addComponent(homeButton)
                    .addComponent(Submit)
                    .addComponent(Save))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 623, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundLain1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundLain1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        // TODO add your handling code here:
        card = (CardLayout) containerPanel.getLayout();
        if (getCurrentCard().getName() != null) {
            if (getCurrentCard().getName().equals("blok56")) {
                if(loop < jumlahPekerja){
                kuesionerController.resetBlok56();
                loop++;
                Blok56.addListEntriBlok56(loop-2, Blok56.getModel());
                JOptionPane.showMessageDialog(this, "Lanjut nginput entri ke-"+loop);
                
                }
                else{
                    JOptionPane.showMessageDialog(this, "Sudah selesai menginput "+loop+" entri \nSilakan submit");
                }
            }
        } else {
            card.next(containerPanel);
        }
        jScrollPane1.getViewport().setViewPosition(new java.awt.Point(0, 0));
    }//GEN-LAST:event_nextButtonActionPerformed

    private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousButtonActionPerformed
        // TODO add your handling code here:
        card = (CardLayout) containerPanel.getLayout();
        card.previous(containerPanel);
        jScrollPane1.getViewport().setViewPosition(new java.awt.Point(0, 0));
    }//GEN-LAST:event_previousButtonActionPerformed

    private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonActionPerformed
        // TODO add your handling code here:
        int x = JOptionPane.showConfirmDialog(this, "Reset?", "Confirm", 0);
        if (x == 0) {
            kuesionerController.resetAll();
            card = (CardLayout) panel.getLayout();
            card.show(panel, "homePanel");
        }
    }//GEN-LAST:event_homeButtonActionPerformed

    private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed
        // TODO add your handling code here:
        BackupKuesioner.write();
        JOptionPane.showMessageDialog(this, "Save success!");
    }//GEN-LAST:event_SaveActionPerformed

    private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitActionPerformed
        try {
            // TODO add your handling code here:
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("pkl53PU");
            B1cobaJpaController b1jc = new B1cobaJpaController(emf);
            B5cobaJpaController b56jc = new B5cobaJpaController(emf);
            b1jc.create(Blok123.getModel());
            b56jc.create(Blok56.getModel());
            for (int i = 0; i < jumlahPekerja-1; i++) {
                b56jc.create(Blok56.getlistEntriBlok56().get(i));
            }
            JOptionPane.showMessageDialog(this, "JOS");
        } catch (Exception ex) {
            Logger.getLogger(DataEntriPekerjaanPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_SubmitActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Save;
    private javax.swing.JButton Submit;
    private pkl53.model.B1coba b1coba1;
    private pkl53.model.B5coba b5coba1;
    private pkl53.imageClass.BackgroundLain backgroundLain1;
    private static pkl53.view.Blok123 blok1231;
    private static pkl53.view.Blok4 blok41;
    private static pkl53.view.Blok56 blok561;
    private static pkl53.view.Blok5678 blok56781;
    private pkl53.model.Blok5678coba blok5678coba1;
    private static javax.swing.JPanel containerPanel;
    private javax.swing.JButton homeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton previousButton;
    // End of variables declaration//GEN-END:variables

    public static Blok123 getBlok123() {
        return blok1231;
    }

    public static Blok4 getBlok4() {
        return blok41;
    }

    public static Blok56 getBlok56() {
        return blok561;
    }
    
    public static Blok5678 getBlok5678() {
        return blok56781;
    }
    
    public static JPanel getContainerPanel() {
        return containerPanel;
    }

    public JPanel getCurrentCard() {
        JPanel card = null;

        for (Component comp : containerPanel.getComponents()) {
            if (comp.isVisible() == true) {
                card = (JPanel) comp;
            }
        }

        return card;
    }
}
